﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AzureGames.Data;
using AzureGames.Domain.Contracts;
using AzureGames.Domain.Models;
using AzureGames.Model;

namespace AzureGames.Domain.Handlers
{
    public class UpdateGameUser
    {
        private readonly IUserRepository _userRepository;

        public UpdateGameUser(IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }

        public virtual void Execute(GameUser gameUser)
        {
            try
            {
                Model.User user = new User(gameUser.ID, gameUser.Name)
                {
                    AvatarImage = gameUser.AvatarImage,
                    Email = gameUser.Email,
                    FirstName = gameUser.FirstName,
                    LastName = gameUser.LastName,
                };

                _userRepository.UpdateUser(user);
            }
            catch (InvalidOperationException e)
            {
                throw new BusinessServicesException("Unable to retrieve user from database.", e);
            }
        }
    }
}
